package com.note.feng.juc.mutithread;

import lombok.Getter;

import java.util.concurrent.CountDownLatch;

/**
 * @author yuhf
 */
public class SynchronizedDemo {

    public static void main(String[] args) throws InterruptedException {
        int MAX_TREAD = 10;
        int MAX_TURN = 1000;
        CountDownLatch latch = new CountDownLatch(MAX_TREAD);
        SafeMethodPlus plus = new SafeMethodPlus();
        for(int i = 0; i < MAX_TURN; i ++){
            new Thread(() -> {
                for (int j = 0; j < MAX_TREAD; j++) {
                    plus.selfPlus();
                }
                latch.countDown();
            }).start();
        }
        latch.await();
        System.out.println(plus.getAmount());
        CountDownLatch latch1 = new CountDownLatch(MAX_TREAD);
        for(int i = 0; i < MAX_TURN; i ++){
            new Thread(() -> {
                for (int j = 0; j < MAX_TREAD; j++) {
                    SafeStaticMethodPlus.staticSelfPlus();
                }
                latch1.countDown();
            }).start();
        }
        latch1.await();
        System.out.println(SafeStaticMethodPlus.amount);
    }

    @Getter
    public static class SafeMethodPlus{
        private Integer amount = 0;

        public void selfPlus(){
            amount ++;
        }

    }

    @Getter
    public static class SafeStaticMethodPlus{
        private static Integer amount = 0;

        public void selfPlus(){
            amount ++;
        }

        public static void staticSelfPlus(){
            amount ++;
        }
    }
}


